package com.molyfun.parents.activity.report.plan;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseFileDownloadActivity;
import com.molyfun.parents.activity.report.view.PlanDetailView;
import com.molyfun.parents.rest.bean.PlanDetailResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.LogUtils;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.view.FileListView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PlanDetailActivity extends BaseFileDownloadActivity implements View.OnClickListener {

    @InjectView(R.id.base_title_left)
    ImageView mTitleBack;
    @InjectView(R.id.base_title)
    TextView mTitle;

    @InjectView(R.id.plan_time_space)
    TextView mTimeSpace;
    @InjectView(R.id.plan_detail_view)
    PlanDetailView mPlanDetailView;//添加数据
    @InjectView(R.id.layout_field_list)
    LinearLayout mFieldList;
    @InjectView(R.id.file_list_view)
    FileListView mPlanFileLayout;

    private String mPlanId;
    private String mMsgId;
    private String mTeacherName;

    private static final String FLAG_TEACHER_NAME = "teacher_name";

    public static void startActivity(Context context, String planId) {
        Intent intent = new Intent();
        intent.putExtra(Const.PLAN_ID, planId);
        intent.setClass(context, PlanDetailActivity.class);
        context.startActivity(intent);
    }

    public static void startFromClassRoomActivity(Context context, String planId, String teacherName) {
        Intent intent = new Intent();
        intent.putExtra(Const.PLAN_ID, planId);
        intent.putExtra(FLAG_TEACHER_NAME, teacherName);
        intent.setClass(context, PlanDetailActivity.class);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String planId, String mMsgId) {
        Intent intent = new Intent();
        intent.putExtra(Const.PLAN_ID, planId);
        intent.putExtra(Const.MSG_ID, mMsgId);
        intent.setClass(context, PlanDetailActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_detail);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");

        mTitle.setText("计划详情");

        mTitleBack.setOnClickListener(this);

        mPlanId = getIntent().getStringExtra(Const.PLAN_ID);
        LogUtils.i("PlanDetailActivity", mPlanId);
        mMsgId = getIntent().getStringExtra(Const.MSG_ID);
        mTeacherName = getIntent().getStringExtra(FLAG_TEACHER_NAME);
        if(!TextUtils.isEmpty(mTeacherName)){
            mTitle.setText("计划详情(" + mTeacherName + ")");
        }
        getPlanDetail(mPlanId);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
        }
    }

    private void getPlanDetail(String planId) {
        LogUtils.i("PlanDetailActivity----->", planId);
        showProgressDialog();
        Call<PlanDetailResponse> call = MFPApplication.getInstance().getRestClient().getTrainingInfo(planId);
        call.enqueue(new Callback<PlanDetailResponse>() {
            @Override
            public void onResponse(Call<PlanDetailResponse> call, Response<PlanDetailResponse> response) {
                dismissProgressDialog();
                if (response != null && response.body() != null && response.body().getData() != null) {
                    PlanDetailResponse.PlanDetailBean planDetailBean = response.body().getData();
                    mTimeSpace.setText(TimeUtils.getDateByTimeMillis(planDetailBean.getBegindatetime()) + "~" + TimeUtils.getDateByTimeMillis(planDetailBean.getEnddatetime()));
                    if (planDetailBean.getFieldlist() != null && planDetailBean.getFieldlist().size() > 0) {
                        for (final PlanDetailResponse.PlanDetailBean.FieldlistBean fieldlistBean : planDetailBean.getFieldlist()) {
                            LinearLayout fieldView = (LinearLayout) LayoutInflater.from(PlanDetailActivity.this).inflate(R.layout.view_plan_field_cell, null, false);
                            ((TextView) fieldView.findViewById(R.id.field_name)).setText(fieldlistBean.getFieldtempname());
                            fieldView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    FieldDetailActivity.startActivity(PlanDetailActivity.this, fieldlistBean);
                                }
                            });
//                            mFieldList.addView(fieldView);//
                            mPlanDetailView.initData(mPlanId, planDetailBean.getFieldlist());//view传数据

                        }
                        List<PlanDetailResponse.PlanDetailBean.FieldlistBean> fieldlist = planDetailBean.getFieldlist();

                    }
                    //TODO 数据
//
                    if (planDetailBean.getDoclist() != null && planDetailBean.getDoclist().size() > 0) {
                        mPlanFileLayout.initDataList(planDetailBean.getDoclist());
//                        for (final PlanDetailResponse.PlanDetailBean.AnalysislistBean analysislistBean : planDetailBean.getDoclist()) {
//                            LinearLayout fileLayout = (LinearLayout) LayoutInflater.from(PlanDetailActivity.this).inflate(R.layout.layout_report_analyze_file_cell, null, false);
//                            ((TextView) fileLayout.findViewById(R.id.file_name)).setText(analysislistBean.getName());
//                            fileLayout.setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    if ((analysislistBean.getName().toLowerCase()).trim().contains("jpg") || analysislistBean.getName().toLowerCase().trim().contains("png")) {
//                                        ImageViewerActivity.startActivity(PlanDetailActivity.this, analysislistBean.getUrl());
//                                    } else {
//                                        GameViewerActivity.startActivity(PlanDetailActivity.this, RestClient.ONLINE_OFFICE_FILE_URL_HEAD + analysislistBean.getUrl());
////                                        downloadFeil(analysislistBean.getUrl(), analysislistBean.getName());
//                                    }
//                                }
//                            });
//                            mPlanFileLayout.addView(fileLayout);
//                        }
                    }
                }

            }

            @Override
            public void onFailure(Call<PlanDetailResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

}
